<%--
  Created by IntelliJ IDEA.
  User: 67693
  Date: 2021/7/20
  Time: 15:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>

<head>
    <meta charset="utf-8">
    <title>我的商品</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/library/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/library/css/public.css" media="all">
    <script src="/library/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <div class="demoTable">
            商品名：
            <div class="layui-inline">
                <input class="layui-input" type="text" name="matchString" id="matchString" autocomplete="off">
            </div>
            <button class="layui-btn" lay-submit="" lay-filter="search" data-type="reload">搜索</button>
        </div>


        <!--表单，查询出的数据在这里显示-->
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="update">修改</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="delete">删除</a>
        </script>

    </div>
</div>

<%--<script src="/library/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>--%>
<%--<script src="/library/js/lay-config.js?v=1.0.4" charset="utf-8"></script>--%>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            table = layui.table;
        var form = layui.form,
            layer = layui.layer;
        form.on('submit(search)', function (data) {
            data = data.field;
            if (data.matchString == '') {
                layer.msg('请输入内容');
                return false;
            }
        });
        table.render({
            elem: '#currentTableId',
            url: '/getUserProduct',     //查询全部数据
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {field: 'goods_id', width: 150, title: '商品号'},
                {field: 'goods_name', width: 200, title: '商品名'},
                {field: 'goods_des', width: 200, title: '商品描述'},
                {field: 'goods_price', width: 200, title: '价格（元）'},
                {field: 'goods_count', width: 200, title: '库存'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,  <!--默认显示15条-->
            page: true,
            skin: 'line',
            id: 'testReload'
        });

        var $ = layui.$, active = {
            reload: function () {
                // var goods_id = $('#goods_id').val();
                // var goods_name = $('#goods_name').val();
                // var goods_username = $('#goods_username').val();
                // var goods_des = $('#goods_des').val();
                // var goods_count = $('#goods_count').val();
                // console.log(name)
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        matchString: $('#matchString').val()
                    }
                    , url: "/search"
                    , method: "POST"
                }, 'data');
            }
        };

        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        /**
         * toolbar栏监听事件
         */
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            // 监听添加操作
            if (obj.event === 'update') {
                var index = layer.open({
                    title: '填写商品信息',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['60%', '60%'],
                    content: '/updateProductCount?id=' + data.goods_id + '&name=' + data.goods_name + '&des=' + data.goods_des + '&price=' + data.goods_price + '&count=' + data.goods_count
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }else if (obj.event === 'delete') {  // 监听删除操作
                layer.confirm('确定是否删除', function (index) {
                    //调用删除功能
                    deleteInfoByIds(data.goods_id,index);
                    layer.close(index);
                });
            }

        });
        function deleteInfoByIds(ids ,index){
            //向后台发送请求
            $.ajax({
                url: "/deleteProduct",
                type: "POST",
                data: {id: ids},
                success: function (result) {
                    if (result.code == 0) {//如果成功
                        layer.msg('删除成功', {
                            icon: 6,
                            time: 500
                        }, function () {
                            parent.window.location.reload();
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        });
                    } else {
                        layer.msg("删除失败");
                    }
                }
            })
        };
    });
</script>
</body>
</html>
